package com.zhyz.bookstore.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource ds = null;

    static {

        Properties prop = new Properties();

        try {
            InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            prop.load(in);
            ds = DruidDataSourceFactory.createDataSource(prop);
        } catch ( Exception e) {

            throw new RuntimeException(e);
        }

    }

    public static Connection getconnection() {

        try {
            return ds.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(ResultSet rs, Statement st, Connection conn) {

        try {

            if (rs != null)
                rs.close();
            if (st != null)
                st.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }

    }
}
